--#include "data\config\activityconfig\CombineServer\CombineServerArenaConfig.lua" once

CombineArenaApplyRankColumn = 
{	
	{"name"},				--0，玩家Name
}

CombineArenaScoreColumn = 
{	
	{"name"},				--0，玩家Name
	{"serverId"},			--1，玩家ServerId(留空)
	{"job"},				--2，玩家职业
	{"attack"},				--3，攻击力
}

--大乱斗积分榜
CombineBigFightScoreColumn = 
{	
	{"name"},				--0，玩家Name
	{"serverId"},			--1，玩家ServerId(留空)
	{"job"},				--2，玩家职业
	{"attack"},				--3，攻击力
	{"support"},			--4，被声援数量
}


CombineAreanIdxMax			= 30									--最大30个擂台，应该够了

CombineArenaApplyRank 		= Lang.Rank.CombineArenaApplyRank		--合服擂台报名榜
CombineArenaScoreRank 		= Lang.Rank.CombineArenaScoreRank		--合服擂台积分榜
CombineBigFightScoreRank 	= Lang.Rank.CombineBigFightScoreRank	--合服大乱斗积分榜

COMBINE_ARENA_IDX   		= 0 				--此副本是第几个擂台
COMBINE_ARENA_CAMP1_SCORE   = 1
COMBINE_ARENA_CAMP2_SCORE   = 2

--[[
注册一些事件
]]
function ResisterCombineArenaProc()
	SceneExitDispatcher.register( CombineServerArenaCfg.arena.sceneId,  OnExitCombineArena )				--注册离开场景、副本后触发的事件
	FubenTimeoutDispatcher.register( CombineServerArenaCfg.arena.fubenId, OnCombineArenaTimeout )			--注册场景、副本时间到后触发的事件
	SceneReliveDispatcher.register( CombineServerArenaCfg.arena.sceneId, OnCombineArenaRelive )				--注册场景、副本中角色复活触发的事件
	--SceneHandExitDispatcher.register( CombineServerArenaCfg.arena.sceneId,  OnHandExitCombineArena )		--注册手工离开场景、副本后触发的事件
	--SceneLogoutDispatcher.register( CombineServerArenaCfg.arena.sceneId, OnLogoutCombineArena )			--注册场景、副本中下线触发的事件
	FubenTimeoutDispatcher.register( CombineServerArenaCfg.fight.fubenId, OnCombineArenaFightTimeout )		--注册场景、副本时间到后触发的事件
	SceneReliveDispatcher.register( CombineServerArenaCfg.fight.sceneId, OnCombineArenaFightRelive )		--注册场景、副本中角色复活触发的事件
end

function InitCombineArenaRank()
	--print("InitCombineArenaRank...")
	Rank.Init(CombineArenaApplyRank, CombineArenaApplyRank, CombineArenaApplyRankColumn, 1000, 0)
	Rank.Init(CombineArenaScoreRank, CombineArenaScoreRank, CombineArenaScoreColumn, 1000, 0)
	Rank.Init(CombineBigFightScoreRank, CombineBigFightScoreRank, CombineBigFightScoreColumn, 1000, 0)
end

--[[
本次合服的擂台赛正式开始
1、清除相关排行榜
]]
function CombineArenaStartAndClear()
	--print("CombineArenaStartAndClear...")
	CombineArenaStartClearRank()
end


function CombineArenaStartClearRank()
	print("CombineArenaStartClearRank...")
	ClearCommonRank( CombineArenaApplyRank )
	ClearCommonRank( CombineArenaScoreRank )
	ClearCommonRank( CombineBigFightScoreRank )
end

--[[
已经退出合服擂台
注意：手工退出、下线、被T出，GM指令Move出都会触发
]]
function OnExitCombineArena( sysarg, sceneId, fubenId )
	--print("OnExitCombineArena, sceneId="..sceneId..", fubenId="..fubenId)
	CloseActivityRightPanel(sysarg)
	Actor.setPkMode(sysarg, fpPeaceful)
	Actor.setCampId( sysarg, 0 )
	Actor.updateActorName( sysarg ) 			--更新名字颜色给附近玩家
end

--[[
手工离开场景
1、某方阵营最后一人离开，擂台结束
]]
function OnHandExitCombineArena( sysarg )
	local fubenPtr = Actor.getFubenPrt( sysarg )
	CheckCombineArenaCamp( fubenPtr )
end

--[[
合服擂台副本中下线
1、某方阵营最后一人离开，擂台结束
]]
function OnLogoutCombineArena( sysarg )
	local fubenPtr = Actor.getFubenPrt( sysarg )
	CheckCombineArenaCamp( fubenPtr )
end

function BroadCombineArenaNext( arenaIdx )
	arenaIdx = tonumber( arenaIdx )
	if arenaIdx == 1 then
		System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean031, ttScreenCenter + ttChatWindow )
	elseif arenaIdx == 2 then
		System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean032, ttScreenCenter + ttChatWindow )
	elseif arenaIdx == 3 then
		System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean033, ttScreenCenter + ttChatWindow )
	elseif arenaIdx == 4 then
		System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean034, ttScreenCenter + ttChatWindow )
	elseif arenaIdx == 5 then
		System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean035, ttScreenCenter + ttChatWindow )
	elseif arenaIdx == 6 then
		System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean036, ttScreenCenter + ttChatWindow )
	elseif arenaIdx == 7 then
		System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean037, ttScreenCenter + ttChatWindow )
	elseif arenaIdx == 8 then
		System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean038, ttScreenCenter + ttChatWindow )
	end
end

--[[
该场合服擂台时间到
]]
function OnCombineArenaTimeout( fubenPtr )
	--print("OnCombineArenaTimeout...")
	local fubenId = Fuben.getFuBenId(fubenPtr)
	if fubenId ~= CombineServerArenaCfg.arena.fubenId then
		return
	end

	local camp1Win  = false
	local camp2Win  = false
	local campNum1, campNum2 = GetCombineArenaCampPlayerNum( fubenPtr )
	if campNum1 > 0 and campNum2 == 0 then 		--一方没人，另一方获胜
		camp1Win = true
		camp2Win = false
	elseif campNum1 == 0 and campNum2 > 0 then	--一方没人，另一方获胜
		camp1Win = false
		camp2Win = true
	elseif campNum1 > 0 and campNum2 > 0 then	--双方都有人，按积分算胜负，积分
		local campScore1 = Fuben.getFbValue( fubenPtr, COMBINE_ARENA_CAMP1_SCORE ) or 0
		local campScore2 = Fuben.getFbValue( fubenPtr, COMBINE_ARENA_CAMP2_SCORE ) or 0
		if campScore1 > campScore2 then
			camp1Win = true
			camp2Win = false
		elseif campScore1 < campScore2 then
			camp1Win = false
			camp2Win = true
		else
			camp1Win = false
			camp2Win = false
		end
	end
	CombineArenaOver( fubenPtr, camp1Win, camp2Win )
	local dVar = System.getDyanmicVar()
	if not dVar.combineArena then
		dVar.combineArena = {}
	end

	local arenaIdx = Fuben.getFbValue( fubenPtr, COMBINE_ARENA_IDX ) or 0
	dVar.combineArena[arenaIdx] = nil
	--print("OnCombineArenaTimeout, arena["..arenaIdx.."] timeout")
end

--[[
合服大乱斗结束
]]
function OnCombineArenaFightTimeout( fubenPtr )
	local dVar = System.getDyanmicVar()
	dVar.cmbFightHandle = nil
	SendCombineArenaFightAwards( ) 			--发奖

	local scoreName1  = GetCommonRankColumnValueByIdx(1, CombineBigFightScoreRank, 0)
	local scoreValue1 = GetCommonRankValueByIdx(1, CombineBigFightScoreRank) or 0
	if scoreValue1 > 0 then 		--第1名需要有积分才能发奖
		local msg = string.format( Lang.ScriptTips.CombineServerArean011, scoreName1 )
		System.broadcastTipmsg( msg, ttScreenCenter + ttChatWindow )
	else
		System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean020, ttScreenCenter + ttChatWindow )
	end
end

--[[
合服擂台公共开始报名
]]
function CombineArenaBroadApply( flag )
	flag = tonumber( flag )
	if flag == 1 then
		System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean005, ttScreenCenter + ttChatWindow )
	else
		System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean006, ttScreenCenter + ttChatWindow )
	end
end


--[[
合服擂台公共结束
]]
function CombineArenaBroadOver(  flag )
	flag = tonumber( flag )
	if flag == 1 then
		System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean007, ttScreenCenter + ttChatWindow )
	else
		--System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean008, ttScreenCenter + ttChatWindow )
	end
end

--[[
合服大乱斗公共开始报名
]]
function CombineArenaBroadFight(  flag )
	flag = tonumber( flag )
	if flag == 1 then
		System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean009, ttScreenCenter + ttChatWindow )
	else
		System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean010, ttScreenCenter + ttChatWindow )
	end
end

--是否在合服擂台活动期间
function IsInCombineArenaTime( sysarg )
	local combServerDay = CombineServerArenaCfg.arena.combineServerDay
	if not CheckCombineServerDay( combServerDay[1], combServerDay[2] ) then
		return false
	end

	return CheckTimesValid( CombineServerArenaCfg.arena.activeTime)
end


--是否在合服大乱斗活动期间
function IsInCombineArenaFightTime( sysarg )
	local combServerDay = CombineServerArenaCfg.fight.combineServerDay
	if not CheckCombineServerDay( combServerDay[1], combServerDay[2] ) then
		return false
	end

	return CheckTimesValid( CombineServerArenaCfg.fight.activeTime)
end


function IsInCombineArena( sysarg )
	return Actor.getFubenId( sysarg ) == CombineServerArenaCfg.arena.fubenId 
end

function IsInCombineArenaFight( sysarg )
	return Actor.getFubenId( sysarg ) == CombineServerArenaCfg.fight.fubenId 
end

function HandleCombineArenaApply( sysarg, packet )
	local applyType = DataPack.readChar(packet)
	CombineArenaApply( sysarg, applyType )
end

--[[
报名参加合服擂台
]]
function CombineArenaApply( sysarg, applyType )
	--print("CombineArenaApply, applyType="..applyType)
	if not IsInCombineArenaTime( sysarg ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServerArean016, ttFlyTip)
		return
	end

	local levelLimit = CombineServerArenaCfg.arena.levelLimit
	if not Actor.checkActorLevel( sysarg, levelLimit[2], levelLimit[1] ) then
    	Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0016,ttFlyTip)
    	return
    end

	local actorId = Actor.getActorId( sysarg )
	local applySts = GetCommonRankValueById( actorId, CombineArenaApplyRank )
	if applyType > 0 then			--报名
		if applySts > 0 then
			SendCombineArenaApplySts( sysarg )
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServerArean001, ttFlyTip)
			return
		end
		SetCommonRankValueById( actorId, CombineArenaApplyRank, 1 )
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServerArean003, ttFlyTip)
	else 							--取消报名
		RemoveCommonRankRecord( actorId, CombineArenaApplyRank )							--删除该条记录
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServerArean004, ttFlyTip)
	end
	SendCombineArenaApplySts( sysarg )
end

--[[
合服擂台开始
从报名玩家中一次抽取，开启副本
]]
function CombineArenaBegin( )
	local ranking = Ranking.getRanking( CombineArenaApplyRank )
	if not ranking then
		return
	end

	--local nCount = Rank.getRankItemCount( ranking )
	local nCount = Ranking.getRankItemCountByLimit( ranking, 0 )
	--if nCount <= 0 then
	--	return
	--end

	local dVar = System.getDyanmicVar()
	dVar.combineArena = {}

	local fubenHandle = 0
	local arenaIdx 	  = 0
	local createArean = true
	local playerNum = 0
	local campId 	= 1

	--擂台赛开启，先创建1个擂台副本
	fubenHandle = Fuben.createFuBen( CombineServerArenaCfg.arena.fubenId )
	--print("CombineArenaBegin, fubenHandle="..fubenHandle)
	if fubenHandle <= 0 then
		return
	end
	arenaIdx = 1  		--第1个擂台
	OnCombineArenaCreated( fubenHandle, arenaIdx )
	createArean = false
	for idx=1, nCount do
		local item = Ranking.getItemFromIndex(ranking, idx-1)
		if item then		--当前榜
			local playerId  = Ranking.getId(item)
			local player 	= Actor.getActorById( playerId )
			--print("CombineArenaBegin, playerId="..playerId)
			if player then 		--玩家必须在线
				if Actor.canBeTelported( player ) == enCanntBeTeledReason_No then			--玩家能够被拉传
					Actor.setCampId( player, campId )
					local enterPos 	= CombineServerArenaCfg.arena.enterPos[campId]
					if campId == 1 then			--阵营颠倒
						campId = 2
					else
						campId = 1
					end

					if createArean then			--有玩家需要参加，并且需要创建一个擂台
						fubenHandle = Fuben.createFuBen( CombineServerArenaCfg.arena.fubenId )
						if fubenHandle <= 0 then
							return
						end

						arenaIdx = arenaIdx + 1
						OnCombineArenaCreated( fubenHandle, arenaIdx )
						createArean = false
					end

					if Actor.enterFuBen( player, fubenHandle, CombineServerArenaCfg.arena.sceneId, enterPos[1], enterPos[2]) then
						OnCombineArenaEnter( player )
						playerNum = playerNum + 1
						if playerNum >= CombineServerArenaCfg.arena.playerNum then 	--擂台人数满
							createArean = true 			--要开始新擂台
							playerNum	= 0 			--新擂台玩家数量为0
							campId 		= 1 			--新擂台阵营从1开始
						end
					end
				end
			end
		end
	end

	--额外创建几个擂台，提供给直接点按钮进入的玩家
	for i=1, CombineServerArenaCfg.arena.extraArenaNum do
		local extraArenaHandle = Fuben.createFuBen( CombineServerArenaCfg.arena.fubenId )
		--print("CombineArenaBegin, fubenHandle="..fubenHandle)
		if extraArenaHandle > 0 then
			arenaIdx = arenaIdx + 1
			OnCombineArenaCreated( extraArenaHandle, arenaIdx )
		end
	end

	ClearCommonRank( CombineArenaApplyRank )			--清空报名
	System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean019, ttScreenCenter + ttChatWindow )
end

--[[
手工进入擂台
]]
function CombineArenaEnter( sysarg )
	if not IsInCombineArenaTime( sysarg ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServerArean016, ttFlyTip)
		return
	end

	local levelLimit = CombineServerArenaCfg.arena.levelLimit
	if not Actor.checkActorLevel( sysarg, levelLimit[2], levelLimit[1] ) then
    	Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0016,ttFlyTip)
    	return
    end

	local dVar = System.getDyanmicVar()
	if not dVar.combineArena then 			--擂台尚未开始
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServerArean021, ttFlyTip)
		return
	end

	if not Actor.canTelport( sysarg, CombineServerArenaCfg.arena.sceneId ) then
		return
	end

	for arenaIdx=1, CombineAreanIdxMax do
		local arenaHandle = dVar.combineArena[arenaIdx]
		if DoCombineArenaEnter( sysarg, arenaHandle ) then
			return
		end
	end
	Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServerArean018, ttFlyTip)
end

function DoCombineArenaEnter( sysarg, arenaHandle )
	if not arenaHandle then
		return false
	end

	--print("DoCombineArenaEnter, arenaHandle="..arenaHandle)
	local fubenPtr = Fuben.getFubenPtr( arenaHandle )			--副本已經被撤銷
	if not fubenPtr then
		return false
	end

	if Fuben.getActivityType( fubenPtr ) ~= enFubenActivityType_CombineArena then		--不是合服擂台副本
		return false
	end

	local campNum1, campNum2 = GetCombineArenaCampPlayerNum( fubenPtr )
	--print("DoCombineArenaEnter, campNum1="..campNum1..", campNum2="..campNum2)
	if campNum1 + campNum2 >= CombineServerArenaCfg.arena.playerNum then				--副本人数满了
		return false
	end

	local campId = 1
	if campNum1 > campNum2 then			--阵营1人数大于阵营2人数，则新进来的玩家分配到阵营2
		campId = 2
	end
	--print("DoCombineArenaEnter, campId="..campId)
	Actor.setCampId( sysarg, campId )
	local enterPos 	= CombineServerArenaCfg.arena.enterPos[campId]
	if Actor.enterFuBen( sysarg, arenaHandle, CombineServerArenaCfg.fight.sceneId, enterPos[1], enterPos[2]) then
		OnCombineArenaEnter( sysarg )
		return true
	end
	return false
end
--[[
合服大乱斗开始
]]
function CombineArenaFightBegin( )
	local dVar = System.getDyanmicVar()
	dVar.cmbFightHandle = Fuben.createFuBen( CombineServerArenaCfg.fight.fubenId )
	if dVar.cmbFightHandle <= 0 then
		dVar.cmbFightHandle = nil
		return
	end

	System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean009, ttScreenCenter + ttChatWindow )
end

--[[
合服大乱斗进入
]]
function CombineArenaFightEnter( sysarg )
	if not IsInCombineArenaFightTime( sysarg ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServerArean017, ttFlyTip)
		return
	end

	local levelLimit = CombineServerArenaCfg.fight.levelLimit
	if not Actor.checkActorLevel( sysarg, levelLimit[2], levelLimit[1] ) then
    	Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0016,ttFlyTip)
    	return
    end

	local actorId 	= Actor.getActorId( sysarg )
	local rankIndex = GetCommonRankIndexById(actorId, CombineArenaScoreRank)
	--print("CombineArenaFightEnter, rankIndex="..rankIndex)
	if rankIndex <= 0 or rankIndex > CombineServerArenaCfg.fight.rankIdxLimit then
		local msg = string.format( Lang.ScriptTips.CombineServerArean015, CombineServerArenaCfg.fight.rankIdxLimit )
		Actor.sendTipmsg(sysarg, msg, ttFlyTip)
		return
	end

	local dVar = System.getDyanmicVar()
	if not dVar.cmbFightHandle then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.CombineServerArean022, ttFlyTip)
		return
	end

	if not Fuben.isFubenExist( dVar.cmbFightHandle ) then			--副本已经不存在（被撤销）了
		dVar.cmbFightHandle = nil
		return
	end

	if not Actor.canTelport( sysarg, CombineServerArenaCfg.fight.sceneId ) then
		return
	end

	local idx, enterPos = GetItemIdxRandNoWeight( CombineServerArenaCfg.fight.enterPosRand )
	if Actor.enterFuBen( sysarg, dVar.cmbFightHandle, CombineServerArenaCfg.fight.sceneId, enterPos[1], enterPos[2]) then
		local actorId = Actor.getActorId( sysarg )
		local score = GetCommonRankValueById( actorId, CombineBigFightScoreRank )
		if score == 0 then
			UpdateCommonRankValueById( actorId, CombineBigFightScoreRank, 1 )				--首次进入，加1分，以区别没有参加大乱斗的玩家
		end
		OpenCombineArenaFightPanel( sysarg )
	end
end


function HandleCombineArenaFightSupport( sysarg, packet )
	local applyType = DataPack.readChar(packet)
	CombineArenaFightSupport( sysarg, rankIdx )
end

--[[
进入擂台或者大乱斗
]]
function HandleCombineArenaEnter( sysarg, packet )
	local flag = DataPack.readChar(packet)
	--print("HandleCombineArenaEnter, flag="..flag)
	if flag == 1 then
		CombineArenaEnter( sysarg ) 		--进入擂台
	else
		CombineArenaFightEnter( sysarg )	--进入大乱斗
	end
end

function CombineArenaFightSupport( sysarg, rankIdx )
	
end

function OnCombineArenaCreated( fubenHandle, arenaIdx )
	local dVar = System.getDyanmicVar()
	if not dVar.combineArena then
		dVar.combineArena = {}
	end
	local fubenPtr = Fuben.getFubenPtr( fubenHandle )
	if fubenPtr then
		dVar.combineArena[arenaIdx] = fubenHandle
		Fuben.setActivityType( fubenPtr, enFubenActivityType_CombineArena ) 	--擂台副本，以免误入
		Fuben.setFbValue( fubenPtr, COMBINE_ARENA_IDX, arenaIdx )
		--Fuben.unregScriptCallback(fubenPtr, "OnCombineArenaCheckCamps")
		--Fuben.regScriptTimer2S(fubenPtr, 0, 10000, 10000, 1, "OnCombineArenaCheckCamps")			--10秒后，判断一次阵营人数
		print("ArenaCreated, arena["..arenaIdx.."]="..fubenHandle)
	end
end

--判断一次阵营人数
function OnCombineArenaCheckCamps( fubenPtr )
	CheckCombineArenaCamp( fubenPtr )
end

function OnCombineArenaEnter( sysarg )
	Actor.setPkMode(sysarg, fpZY)
	OpenCombineArenaPanel( sysarg )
end

function OpenCombineArenaPanel( sysarg )
	if not IsInCombineArena( sysarg ) then
		return
	end
	local fubenPtr = Actor.getFubenPrt(sysarg)

	local activityType 	= 1 			--活动
	local activityId 	= enRightPanelActivity_CombServerArean
	local restTime 		= Fuben.getFbTime( fubenPtr )
	if restTime < 0 then
		restTime = 0
	end
	local panelInfo 	= {}
	panelInfo.sts 		= 1  									--进行中
	panelInfo.title 	= Lang.ScriptTips.CombineServerAreanName
	panelInfo.timeTitle = Lang.ScriptTips.panelCont01			--剩余时间
	panelInfo.restTime	= restTime


	local score1 = Fuben.getFbValue( fubenPtr, COMBINE_ARENA_CAMP1_SCORE ) or 0 		--阵营1积分
	local score2 = Fuben.getFbValue( fubenPtr, COMBINE_ARENA_CAMP2_SCORE ) or 0 		--阵营2积分

	panelInfo.contentTitle 	= ""  			--不用显示
	panelInfo.contents 		= {}

	if Actor.getCampId( sysarg ) == 1 then
		panelInfo.contents[1]	= string.format( Lang.ScriptTips.panelCont22, score1 ) 		--本方积分
		panelInfo.contents[2]	= string.format( Lang.ScriptTips.panelCont23, score2 )		--对方积分
	else
		panelInfo.contents[1]	= string.format( Lang.ScriptTips.panelCont22, score2 )
		panelInfo.contents[2]	= string.format( Lang.ScriptTips.panelCont23, score1 )
	end

	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖, 4-查看排行榜
	--panelInfo.buttons[2]	= 4	

	OpenActivityRightPanel( sysarg, activityType, activityId, panelInfo )
end


function ChangeCombineArenaPanel( sysarg, score1, score2 )
	if not IsInCombineArena( sysarg ) then
		return
	end

	local panelInfo 	= {}
	panelInfo.sts 		= 1
	panelInfo.contents 	= {}
	if Actor.getCampId( sysarg ) == 1 then
		panelInfo.contents[1]	= string.format( Lang.ScriptTips.panelCont22, score1 ) 		--本方积分
		panelInfo.contents[2]	= string.format( Lang.ScriptTips.panelCont23, score2 )		--对方积分
	else
		panelInfo.contents[1]	= string.format( Lang.ScriptTips.panelCont22, score2 )
		panelInfo.contents[2]	= string.format( Lang.ScriptTips.panelCont23, score1 )
	end

	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖, 4-查看排行榜
	--panelInfo.buttons[2]	= 4

	ChangeActivityRightPanel( sysarg, panelInfo )
end

--[[
打开大乱斗面板
]]
function OpenCombineArenaFightPanel( sysarg )
	if not IsInCombineArenaFight( sysarg ) then
		return
	end
	local fubenPtr = Actor.getFubenPrt(sysarg)

	local activityType 	= 1 			--活动
	local activityId 	= enRightPanelActivity_CombServerFight
	local restTime 		= Fuben.getFbTime( fubenPtr )
	if restTime < 0 then
		restTime = 0
	end
	local panelInfo 	= {}
	panelInfo.sts 		= 1  									--进行中
	panelInfo.title 	= Lang.ScriptTips.CombineServerFightName
	panelInfo.timeTitle = Lang.ScriptTips.panelCont01			--剩余时间
	panelInfo.restTime	= restTime


	local scoreName1  = GetCommonRankColumnValueByIdx(1, CombineBigFightScoreRank, 0) or Lang.ScriptTips.COM0078
	if scoreName1 == 0 then		--暂无第1名
		scoreName1 = Lang.ScriptTips.COM0078
	end
	local scoreValue1 = GetCommonRankValueByIdx(1, CombineBigFightScoreRank) or 0
	local actorId = Actor.getActorId( sysarg )
	local myRankIdx, myRankValue = GetCommonRankIdxAndValueDataById( actorId, CombineBigFightScoreRank, CombineServerArenaCfg.fight.scoreRank.rankLimit )

	panelInfo.contentTitle 	= ""  			--不用显示
	panelInfo.contents 		= {}
	panelInfo.contents[1]	= string.format( Lang.ScriptTips.panelCont17, 1, scoreName1 )
	panelInfo.contents[2]	= string.format( Lang.ScriptTips.panelCont15, 1, scoreValue1 )
	if myRankIdx <= 0 then
		panelInfo.contents[3]	= Lang.ScriptTips.panelCont19
	else
		panelInfo.contents[3]	= string.format( Lang.ScriptTips.panelCont20, myRankIdx )
	end
	panelInfo.contents[4]	= string.format( Lang.ScriptTips.panelCont16, myRankValue )

	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖, 4-查看排行榜
	panelInfo.buttons[2]	= 4	

	OpenActivityRightPanel( sysarg, activityType, activityId, panelInfo )
end

function ChangeCombineArenaFightPanel( sysarg , scoreName1, scoreValue1 )
	if not IsInCombineArenaFight( sysarg ) then
		return
	end

	local actorId = Actor.getActorId( sysarg )
	local myRankIdx, myRankValue = GetCommonRankIdxAndValueDataById( actorId, CombineBigFightScoreRank, CombineServerArenaCfg.fight.scoreRank.rankLimit )

	local panelInfo 		= {}
	panelInfo.sts 			= 1
	panelInfo.contents 		= {}
	panelInfo.contents[1]	= string.format( Lang.ScriptTips.panelCont17, 1, scoreName1 )
	panelInfo.contents[2]	= string.format( Lang.ScriptTips.panelCont15, 1, scoreValue1 )
	if myRankIdx <= 0 then
		panelInfo.contents[3]	= Lang.ScriptTips.panelCont19
	else
		panelInfo.contents[3]	= string.format( Lang.ScriptTips.panelCont20, myRankIdx )
	end
	panelInfo.contents[4]	= string.format( Lang.ScriptTips.panelCont16, myRankValue )

	panelInfo.buttons   	= {}
	panelInfo.buttons[1]	= 1							--1：退出副本，2-单倍领奖，3-双倍领奖, 4-查看排行榜
	panelInfo.buttons[2]	= 4	

	ChangeActivityRightPanel( sysarg, panelInfo )
end


--[[
全副本同步阵营积分
]]
function BoardCombineArenaScore( fubenPtr )
	local campScore1 = Fuben.getFbValue( fubenPtr, COMBINE_ARENA_CAMP1_SCORE ) or 0
	local campScore2 = Fuben.getFbValue( fubenPtr, COMBINE_ARENA_CAMP2_SCORE ) or 0
	local playerList = LuaHelp.getFbActorList(fubenPtr) or {}
	for i, player in ipairs( playerList or {} ) do
		ChangeCombineArenaPanel( player, campScore1, campScore2 )
	end
end

--[[
全副本同步玩家疾风
]]
function BoardCombineArenaFightScore( fubenPtr )
	local scoreName1  = GetCommonRankColumnValueByIdx(1, CombineBigFightScoreRank, 0) or Lang.ScriptTips.COM0078
	if scoreName1 == 0 then		--暂无第1名
		scoreName1 = Lang.ScriptTips.COM0078
	end
	local scoreValue1 = GetCommonRankValueByIdx(1, CombineBigFightScoreRank) or 0
	local playerList = LuaHelp.getFbActorList(fubenPtr) or {}
	for i, player in ipairs( playerList or {} ) do
		ChangeCombineArenaFightPanel( player, scoreName1, scoreValue1 )
	end
end

--[[
]]
function ProcCombineArenaRank( sysarg, addScore )
	local actorId 	= Actor.getActorId( sysarg )
	--print("ProcCombineArenaRank, actorId="..actorId..", addScore="..addScore)
	local job = Actor.getIntProperty( sysarg, PROP_ACTOR_VOCATION )
	UpdateCommonRankValueById( actorId, CombineArenaScoreRank, addScore )				--积分累加
	SetCommonRankColumnValueById( actorId, CombineArenaScoreRank, 0, 0,  Actor.getName(sysarg) )
	SetCommonRankColumnValueById( actorId, CombineArenaScoreRank, 0, 1,  0 )			--留空，无意义
	SetCommonRankColumnValueById( actorId, CombineArenaScoreRank, 0, 2,  job )
	SetCommonRankColumnValueById( actorId, CombineArenaScoreRank, 0, 3,  Actor.getActorBattlePower(sysarg) )
end

--[[
]]
function ProcCombineArenaFightRank( sysarg, addScore )
	local actorId 	= Actor.getActorId( sysarg )
	--print("ProcCombineArenaFightRank, actorId="..actorId..", addScore="..addScore)
	local job = Actor.getIntProperty( sysarg, PROP_ACTOR_VOCATION )
	UpdateCommonRankValueById( actorId, CombineBigFightScoreRank, addScore )				--积分累加
	SetCommonRankColumnValueById( actorId, CombineBigFightScoreRank, 0, 0,  Actor.getName(sysarg) )
	SetCommonRankColumnValueById( actorId, CombineBigFightScoreRank, 0, 1,  0 )			--留空，无意义
	SetCommonRankColumnValueById( actorId, CombineBigFightScoreRank, 0, 2,  job )
	SetCommonRankColumnValueById( actorId, CombineBigFightScoreRank, 0, 3,  Actor.getActorBattlePower(sysarg) )
end

--[[
下发合服擂台报名状态
]]
function SendCombineArenaApplySts( sysarg )
	--local netPack = DataPack.allocPacket(sysarg,LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sCombineServerAreanSendApply)
	local netPack = DataPack.allocPacket(sysarg, 145, 58)
	if netPack then
		local actorId = Actor.getActorId(sysarg)
		local applySts = GetCommonRankValueById( actorId, CombineArenaApplyRank )
		DataPack.writeChar(netPack, applySts)
		DataPack.flush(netPack) 

		--print("->SendCombineArenaApplySts, actorId="..actorId..", applySts="..applySts)
	end
end

function OnCombineArenaActorDie( sysarg, killer )
	--print("OnCombineArenaActorDie, ...")
	if IsInCombineArena( sysarg ) then
		OnCombineArenaDie( sysarg, killer )
	elseif IsInCombineArenaFight( sysarg ) then
		OnCombineArenaFightDie( sysarg, killer )
	end
end

--[[
合服擂台击杀
]]
function OnCombineArenaDie( sysarg, killer )
	local  campId = Actor.getCampId( killer )
	local  fubenValueId = nil
	if campId == 1 then
		fubenValueId = COMBINE_ARENA_CAMP1_SCORE
	elseif campId == 2 then
		fubenValueId = COMBINE_ARENA_CAMP2_SCORE
	end

	if not fubenValueId then
		return
	end
	local fubenPtr = Actor.getFubenPrt(killer)
	local campScore = Fuben.getFbValue( fubenPtr, fubenValueId ) or 0
	Fuben.setFbValue( fubenPtr, fubenValueId, campScore + CombineServerArenaCfg.arena.score.killScoreToCamp ) 		--给阵营加积分
	ProcCombineArenaRank( killer, CombineServerArenaCfg.arena.score.killScoreToActor )

	BoardCombineArenaScore( fubenPtr )			--广播阵营积分
end

--[[
合服大乱斗击杀
]]
function OnCombineArenaFightDie( sysarg, killer )
	local fubenPtr = Actor.getFubenPrt(killer)
	ProcCombineArenaFightRank( killer, CombineServerArenaCfg.fight.killAddScore )
	BoardCombineArenaFightScore( fubenPtr )
end

function GetCombineArenaCampPlayerNum( fubenPtr )
	local campNum1 = 0
	local campNum2 = 0
	local playerList = LuaHelp.getFbActorList(fubenPtr) or {}
	for i, player in ipairs( playerList or {} ) do
		local campId = Actor.getCampId( player )
		--print("CheckCombineArenaCamp, campId="..campId)
		if campId == 1 then
			campNum1 = campNum1 + 1
		elseif campId == 2 then
			campNum2 = campNum2 + 1
		end
	end
	return campNum1, campNum2
end

--[[
检查擂台赛阵营双方人数，如有一方人数为0，另一方大于0，则判断另一方获胜
副本开启10秒后，或者有人离开副本时判断一次
]]
function CheckCombineArenaCamp( fubenPtr )
	--print("CheckCombineArenaCamp...")
	local campNum1, campNum2 = GetCombineArenaCampPlayerNum( fubenPtr )
	--print("CheckCombineArenaCamp, campNum1="..campNum1..", campNum2="..campNum2)
	local camp1Win = false
	local camp2Win = false
	if campNum1 > 0 and campNum2 > 0 then			--双方都有玩家，擂台继续
		return
	elseif campNum1 > 0 and campNum2 == 0 then
		camp1Win = true
		camp2Win = false
	elseif campNum1 == 0 and campNum2 > 0 then
		camp1Win = false
		camp2Win = true
	end

	CombineArenaOver( fubenPtr, camp1Win, camp2Win )			--擂台结束，发奖
	TickCombineArenaPlayerOut( fubenPtr )						--T出擂台副本玩家

	local fubenHandle 	= Fuben.getFubenHandle( fubenPtr )
	Fuben.setFubenTime( fubenHandle, 1 )				--一方没有人，关闭副本，要先T出玩家，再结束副本
end

--踢出活动玩家
function TickCombineArenaPlayerOut( fubenPtr )
	local playerList = LuaHelp.getFbActorList(fubenPtr) or {}
	for i, player in ipairs( playerList or {} ) do
		Actor.exitFuben( player )
	end
end

--[[
camp1Win: true-阵营1胜利， false-阵营1失败
camp2Win: true-阵营2胜利， false-阵营2失败
]]
function CombineArenaOver( fubenPtr, camp1Win, camp2Win )
	local playerList = LuaHelp.getFbActorList(fubenPtr) or {}
	for i, player in ipairs( playerList or {} ) do
		if Actor.getCampId( player ) == 1 and camp1Win then				--本方胜利
			CombineArenaAwardWin( player )
		elseif Actor.getCampId( player ) == 2 and camp2Win then			--本方胜利
			CombineArenaAwardWin( player )
		else 		--本方失败
			CombineArenaAwardLose( player )
		end
	end
end

--[[
合服擂台奖励
]]
function CombineArenaAwardWin( sysarg )
	--print("CombineArenaAwardWin...")
	ProcCombineArenaRank( sysarg, CombineServerArenaCfg.arena.score.winScore )
	local actorId = Actor.getActorId( sysarg )
	CombineArenaAwardMailAward( actorId, Lang.ScriptTips.CombineAreanMailTitle1, Lang.ScriptTips.CombineAreanMailCont1, 
		CombineServerArenaCfg.arena.winAwards )

	local panelInfo = {}
	panelInfo.subActivityId = 0
	panelInfo.sts 		= 1 			--胜利
	panelInfo.awards 	= CombineServerArenaCfg.arena.winAwards
	PassActivityRightPanel( sysarg, 1, enRightPanelActivity_CombServerArean, panelInfo )
end

function CombineArenaAwardLose( sysarg )
	--print("CombineArenaAwardLose...")
	ProcCombineArenaRank( sysarg, CombineServerArenaCfg.arena.score.loseScore )
	local actorId = Actor.getActorId( sysarg )
	CombineArenaAwardMailAward( actorId, Lang.ScriptTips.CombineAreanMailTitle2, Lang.ScriptTips.CombineAreanMailCont2, 
		CombineServerArenaCfg.arena.loseAwards )

	local panelInfo = {}
	panelInfo.subActivityId = 0
	panelInfo.sts 		= 0 			--失败
	panelInfo.awards 	= CombineServerArenaCfg.arena.loseAwards
	PassActivityRightPanel( sysarg, 1, enRightPanelActivity_CombServerArean, panelInfo )
end

function CombineArenaAwardMailAward( actorId, title, content, awards )
	Awards.GiveToMailById( actorId, awards, title, content, GameLog.clCombineServerAreanAward, Lang.ScriptTips.CombineServerAreanLog01 )
end

--[[
合服擂台复活
]]
function OnCombineArenaRelive( sysarg, reliveType  )
	local idx, pos = GetItemIdxRandNoWeight( CombineServerArenaCfg.arena.relivePos )
	--print("OnCombineArenaRelive, posX="..pos[1]..", posY="..pos[2])
	if pos[1] > 0 and pos[2] > 0 then
		local fubenHandle = Actor.getFubenHandle( sysarg )
		Actor.enterFuBen( sysarg, fubenHandle, CombineServerArenaCfg.arena.sceneId, pos[1], pos[2] )
	end
end

--[[
合服大乱斗复活
]]
function OnCombineArenaFightRelive( sysarg, reliveType )
	local idx, pos = GetItemIdxRandNoWeight( CombineServerArenaCfg.fight.relivePos )
	--print("OnCombineArenaRelive, posX="..pos[1]..", posY="..pos[2])
	if pos[1] > 0 and pos[2] > 0 then
		local fubenHandle = Actor.getFubenHandle( sysarg )
		Actor.enterFuBen( sysarg, fubenHandle, CombineServerArenaCfg.fight.sceneId, pos[1], pos[2] )
	end
end

--[[
发送擂台排行榜奖励
]]
function SendCombineArenaRankAwards( )
	--SendCommonRankAwards( enCommonRankType_CombineArena, CombineServerArenaCfg.arena.scoreRank ) 	--发奖
	SendCommonRankAwards( enCommonRankType_CombineArena )
	--将CombineArenaScoreRank的前10名拷贝到CombineBigFightScoreRank中
	local columnNum = 4
	local bSavePoint = false  		--point清0
	CopyCommonRankByRankIdx( CombineArenaScoreRank, CombineBigFightScoreRank, CombineServerArenaCfg.fight.rankIdxLimit, columnNum, bSavePoint )
	SaveCommonRank( CombineBigFightScoreRank )

	System.broadcastTipmsg( Lang.ScriptTips.CombineServerArean008, ttScreenCenter + ttChatWindow )
end


--[[
发送大乱斗排行榜奖励
]]
function SendCombineArenaFightAwards( )
	--SendCommonRankAwards( enCommonRankType_CombineBigFight, CombineServerArenaCfg.fight.scoreRank ) 	--发奖
	SendCommonRankAwards( enCommonRankType_CombineBigFight )
end


--[[
发送合服擂台数据
]]
function SendCombineServerAreanData( sysarg )
	SendCombineArenaApplySts( sysarg )
	--SendCommonRankSimpleData( sysarg, enCommonRankType_CombineArena, CombineServerArenaCfg.arena.scoreRank )
	--SendCommonRankSimpleData( sysarg, enCommonRankType_CombineBigFight, CombineServerArenaCfg.fight.scoreRank )
	SendCommonRankSimpleData( sysarg, enCommonRankType_CombineArena )
	SendCommonRankSimpleData( sysarg, enCommonRankType_CombineBigFight )
end

ResisterCombineArenaProc()
InitCombineArenaRank()
